package com.rul.mars.product.controller;

import cn.hutool.core.bean.BeanUtil;
import com.rul.mars.common.api.CommonPage;
import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.ProductBrandDetail;
import com.rul.mars.mbg.model.ProductBrand;
import com.rul.mars.product.service.ProductBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品品牌服务接口
 *
 * @author RuL
 */
@Api(tags = "ProductBrandController", description = "商品品牌服务")
@RestController
@RequestMapping("/product/brand")
public class ProductBrandController {

    @Autowired
    private ProductBrandService productBrandService;

    /**
     * 分页方式查询商品品牌详细信息
     *
     * @param pageNum  页号
     * @param pageSize 页面大小
     * @return 商品品牌详细信息
     */
    @ApiOperation("分页方式查询商品品牌详细信息")
    @GetMapping("/search")
    public CommonResult<CommonPage<ProductBrandDetail>> search(@RequestParam(required = false, defaultValue = "") String keywords,
                                                               @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                               @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        //查询商品信息
        List<ProductBrand> productBrands = productBrandService.search(keywords, pageNum, pageSize);
        //转为ProductBrandDetail
        List<ProductBrandDetail> brandDetails = productBrands.stream()
                .map(item -> BeanUtil.copyProperties(item, ProductBrandDetail.class))
                .collect(Collectors.toList());
        //转为CommonPage
        CommonPage<ProductBrandDetail> page = CommonPage.restPage(brandDetails);
        return CommonResult.success(page);
    }

}
